<!-- HTML for custom Swagger UI -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>API文档</title>
    <link rel="stylesheet" type="text/css" href="/webjars/swagger-ui/swagger-ui.css">
    <style>
        .token-input-container {
            position: fixed;
            right: 20px;
            top: 10px;
            z-index: 999;
            display: flex;
            gap: 10px;
        }
        .token-input {
            padding: 5px 10px;
            border: 1px solid #4990e2;
            border-radius: 4px;
            width: 300px;
        }
        .token-button {
            background-color: #4990e2;
            color: white;
            padding: 5px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="token-input-container">
    <input type="text" id="tokenInput" class="token-input" placeholder="请输入token...">
    <button onclick="applyToken()" class="token-button">应用Token</button>
</div>
<div id="swagger-ui"></div>

<script src="/webjars/swagger-ui/swagger-ui-bundle.js"></script>
<script src="/webjars/swagger-ui/swagger-ui-standalone-preset.js"></script>
<script>
    window.onload = function() {
        const ui = SwaggerUIBundle({
            url: "/v3/api-docs",
            dom_id: '#swagger-ui',
            deepLinking: true,
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],
            plugins: [
                SwaggerUIBundle.plugins.DownloadUrl
            ],
            layout: "BaseLayout",
            docExpansion: "list",  // 展开所有API
            onComplete: function() {
                // 从localStorage加载上次保存的token
                const savedToken = localStorage.getItem('swagger_token');
                if (savedToken) {
                    document.getElementById('tokenInput').value = savedToken;
                    applyToken();
                }
            }
        });
        window.ui = ui;
    }

    function applyToken() {
        const token = document.getElementById('tokenInput').value;
        if (token) {
            // 保存token到localStorage
            localStorage.setItem('swagger_token', token);
            // 应用token到swagger-ui
            window.ui.preauthorizeApiKey("JWT Authentication", token);
        }
    }
</script>
</body>
</html>